#include "ReportGenerator.h"
#include <AnalyzerHelpers.h>
#include "HD44780AnalyzerResults.h"
#include <vector>
#include "PluginConstants.h"
#include "HD44780AnalyzerSettings.h"
#include "PluginConstants.h"
#include <iostream>
#include <fstream>
#include "ResultAnalyzer.h"
#include <iostream>
#include <sstream>


void CSVReportGenerator::GenerateReport( const char* TargetFile, DisplayBase TargetDisplayBase )
{
    std::ofstream FileStream;
    FileStream.open( TargetFile, std::ios::out );

    if ( !FileStream.is_open() )
    {
        return;
    }

    U64 trigger_sample = GetParent()->GetTriggerSample();
    U32 sample_rate = GetParent()->GetSampleRate();

    FileStream << "Time [s],Value" << std::endl;

    U64 num_frames = GetParent()->GetNumFrames();
    for( U32 i=0; i < num_frames; i++ )
    {
        Frame frame = GetParent()->GetFrame( i );

        char time_str[128];
        AnalyzerHelpers::GetTimeString( frame.mStartingSampleInclusive, trigger_sample, sample_rate, time_str, 128 );

        char number_str[128];
        AnalyzerHelpers::GetNumberString( frame.mData1, TargetDisplayBase, 8, number_str, 128 );

        FileStream << time_str << "," << number_str << std::endl;

        if( GetParent()->UpdateExportProgressAndCheckForCancel2( i, num_frames ) == true )
        {
            FileStream.close();
            return;
        }
    }

    FileStream.close();
}

void SequenceDiagramReportGenerator::GenerateReport( const char* TargetFile, DisplayBase TargetDisplayBase )
{
    std::ofstream file_stream( TargetFile, std::ios::out );
    U64 trigger_sample = GetParent()->GetTriggerSample();
    U32 sample_rate = GetParent()->GetSampleRate();
    file_stream << "title HD44780 Sequence Diagram (" << GetSettings()->GetModeName() << ")";
    file_stream << std::endl;

    U64 num_frames = GetParent()->GetNumFrames();
    S64 LastClockTick = 0;
    for( U64 i=0; i < num_frames; i++ )
    {
        Frame TargetFrame = GetParent()->GetFrame( i );
        file_stream << "Micro->HD44780:" << GetParent()->GetAnalyzer(TargetFrame.mType)->Analyze( TargetFrame )  << std::endl;
        file_stream << "activate HD44780" << std::endl;
        file_stream << "HD44780->Micro:" << std::endl;
        file_stream << "deactivate HD44780" << std::endl;
        file_stream << "note over Micro,HD44780" << std::endl;

        char time_str[MAX_BUFFER_SIZE];
        file_stream << "Time Since Last Tick:";
        if ( ((S64)0 == LastClockTick) )
            file_stream << 0 << std::endl;
        else
        {
            AnalyzerHelpers::GetTimeString( TargetFrame.mStartingSampleInclusive - LastClockTick, trigger_sample, sample_rate, time_str, MAX_BUFFER_SIZE ) ;
            file_stream << time_str << std::endl;
        }

        AnalyzerHelpers::GetTimeString( TargetFrame.mStartingSampleInclusive, trigger_sample, sample_rate, time_str, MAX_BUFFER_SIZE );      
        file_stream << "Clock Start:" << time_str << std::endl;
        AnalyzerHelpers::GetTimeString( TargetFrame.mEndingSampleInclusive - TargetFrame.mStartingSampleInclusive, trigger_sample, sample_rate, time_str, MAX_BUFFER_SIZE );
        file_stream << "Pulse Width:" << time_str << std::endl;

        char number_str[MAX_BUFFER_SIZE];
        AnalyzerHelpers::GetNumberString( TargetFrame.mData1, TargetDisplayBase, 8, number_str, MAX_BUFFER_SIZE );
        file_stream << "Raw Data:" << number_str << std::endl;
        file_stream << "end note" << std::endl << std::endl;

        LastClockTick = TargetFrame.mEndingSampleInclusive;
        if( GetParent()->UpdateExportProgressAndCheckForCancel2( i, num_frames ) == true )
        {
            file_stream.close();
            return;
        }
    }

    file_stream.close();
}

void HTMLReportGenerator::GenerateReport( const char* TargetFile, DisplayBase TargetDisplayBase )
{

}